/*/*用于展示购物车中的单个商品项，并提供数量调整和删除功能。*/*/
<template>   
  <div class="cart-item">  
    <!-- 商品图片 -->
    <div class="item-image">   
      <img :src="item.image" :alt="item.name" />    
    </div>
    <!-- 商品信息（名称+价格） -->
    <div class="item-info">       
      <h4 class="item-name">{{ item.name }}</h4>
      <p class="item-price">¥{{ item.price.toLocaleString() }}</p>
    </div>
    <!-- 数量控制按钮 -->
    <div class="quantity-controls">       
      <button 
        class="quantity-btn" 
        @click="decreaseQuantity"
        :disabled="item.quantity <= 1"
      >
        -
      </button>
      <span class="quantity">{{ item.quantity }}</span>
      <button 
        class="quantity-btn" 
        @click="increaseQuantity"
      >
        +
      </button>
    </div>
    <!-- 小计与删除按钮 -->
    <div class="item-total">      
      <p class="total-price">¥{{ (item.price * item.quantity).toLocaleString() }}</p>
      <button class="remove-btn" @click="removeItem">删除</button>
    </div>
  </div>
</template>

<script setup>
import { useCartStore } from '../stores/cart'    /*引入购物车状态管理模块，用于操作购物车数据*/

const props = defineProps({      /*声明接收父组件传递的item对象，包含商品信息（ID、名称、价格、图片、数量等）*/
  item: {
    type: Object,
    required: true
  }
})

const cartStore = useCartStore()    /*获取购物车状态*/

const increaseQuantity = () => {
  cartStore.updateQuantity(props.item.id, props.item.quantity + 1)
}

const decreaseQuantity = () => {
  if (props.item.quantity > 1) {
    cartStore.updateQuantity(props.item.id, props.item.quantity - 1)
  }
}

const removeItem = () => {
  if (confirm(`确定要删除 ${props.item.name} 吗？`)) {
    cartStore.removeFromCart(props.item.id)     /*传入商品 ID 删除该商品*/
  }
}
</script>

<style scoped>   
.cart-item {     /*基础布局*/
  display: flex;
  align-items: center;
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background: white;
  margin-bottom: 12px;
  gap: 16px;
}

.item-image {
  flex-shrink: 0;
}

.item-image img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 6px;
}

.item-info {
  flex: 1;
}

.item-name {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 8px 0;
  color: #333;
}

.item-price {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.quantity-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.quantity-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease;
}

.quantity-btn:hover:not(:disabled) {
  background-color: #f5f5f5;
}

.quantity-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity {
  font-size: 16px;
  font-weight: 600;
  min-width: 24px;
  text-align: center;
}

.item-total {
  text-align: right;
  flex-shrink: 0;
}

.total-price {
  font-size: 18px;
  font-weight: bold;
  color: #e74c3c;
  margin: 0 0 8px 0;
}

.remove-btn {
  padding: 6px 12px;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.2s ease;
}

.remove-btn:hover {
  background-color: #c82333;
}

@media (max-width: 768px) {
  .cart-item {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .item-image {
    align-self: center;
  }
  
  .quantity-controls {
    justify-content: center;
  }
  
  .item-total {
    text-align: center;
  }
}
</style> 